package com.vq3.digitalizationsoftwareintegrates.Servlet.Index;


import com.vq3.digitalizationsoftwareintegrates.Entity.Index.SoftwaresEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Info.ResultInfo;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.UserEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.User_RoleEntity;
import com.vq3.digitalizationsoftwareintegrates.Service.Index.IndexSoftwareService;

import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.Impl.PackagedDataImpl;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.PackagedData;
import com.vq3.digitalizationsoftwareintegrates.Utils.PermissionControl.PermissionControl;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("index")
@Import({ResultInfo.class,PermissionControl.class})
public class Index {

    final IndexSoftwareService indexSoftwareService;
    final ResultInfo resultInfo;
    final PermissionControl permissionControl;

    public Index(IndexSoftwareService indexSoftwareService, ResultInfo resultInfo, PermissionControl permissionControl) {
        this.indexSoftwareService = indexSoftwareService;
        this.resultInfo = resultInfo;
        this.permissionControl = permissionControl;
    }



    @RequestMapping("/findAllSoftware")
    public void findAllSoftware(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        PackagedData packagedData = new PackagedDataImpl();
        UserEntity userEntity = (UserEntity)httpServletRequest.getSession().getAttribute("USER");
        List<User_RoleEntity> user_roleEntities = indexSoftwareService.uidFindRid(userEntity.getUid());


        List<SoftwaresEntity> softwareEntityList = indexSoftwareService.findAll(user_roleEntities);
        resultInfo.setFlag(true);
        resultInfo.setData(softwareEntityList);
        System.out.println(softwareEntityList);


        packagedData.writeValue(resultInfo,httpServletResponse);
    }
    @RequestMapping("/userStatus")
    public void userStatus(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        UserEntity loginUserEntity = permissionControl.getLoginUser(httpServletRequest, httpServletResponse, httpServletRequest.getSession());
        PackagedData packagedData = new PackagedDataImpl();
        packagedData.writeValue(loginUserEntity,httpServletResponse);
    }

}
